package com.srs.backend.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.backend.mapper.FoodMapper;
import com.srs.backend.mapper.FoodMaterialMapper;
import com.srs.backend.mapper.MaterialMapper;
import com.srs.backend.pojo.Food;
import com.srs.backend.pojo.FoodMaterial;
import com.srs.backend.pojo.Material;
import com.srs.backend.service.FoodMaterialService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class FoodMaterialServiceImpl implements FoodMaterialService {
    @Resource
    private FoodMapper foodMapper;
    @Resource
    private FoodMaterialMapper foodMaterialMapper;
    @Resource
    private MaterialMapper materialMapper;

    @Override
    public Result add(Map<String, String> data) {
        String foodIdStr = data.get("food_id");
        String materialIdStr = data.get("material_id");
        String materialNumStr = data.get("material_num");
        Integer materialNum;
        Integer foodId;
        Integer materialId;
        try {
            foodId = Integer.parseInt(foodIdStr);
            materialId = Integer.parseInt(materialIdStr);
            materialNum = Integer.parseInt(materialNumStr);
        } catch (NumberFormatException e) {
            return Result.error("菜品或原材料 数据格式有误");
        }

        if (materialNum < 0  || materialNum > 10000) {
            return Result.error("食材数量有误");
        }

        Food curFood = foodMapper.selectById(foodId);
        Material curMaterial = materialMapper.selectById(materialId);

        if (curFood == null) {
            return Result.error("菜品不存在或已被删除");
        }
        if (curMaterial == null) {
            return Result.error("原材料不存在或已被删除");
        }

        QueryWrapper<FoodMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("food_id", foodId);
        queryWrapper.eq("material_id", materialId);
        List<FoodMaterial> foodMaterialList = foodMaterialMapper.selectList(queryWrapper);
        if (!foodMaterialList.isEmpty()) {
            return Result.error("当前 菜品-原材料 关系已存在");
        }

        foodMaterialMapper.insert(new FoodMaterial(null, foodId, materialId, curFood.getName(), curMaterial.getName(), materialNum));

        return Result.ok();
    }

    @Override
    public Result delete(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        FoodMaterial cur = foodMaterialMapper.selectById(id);
        if (cur == null) {
            return Result.error("当前菜品-原材料的对应已被删除或不存在");
        }
        foodMaterialMapper.deleteById(cur);
        return Result.ok();
    }

    @Override
    public Result modify(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        FoodMaterial cur = foodMaterialMapper.selectById(id);
        if (cur == null) {
            return Result.error("当前菜品-原材料的对应已被删除或不存在");
        }

        String foodIdStr = data.get("food_id");
        String materialIdStr = data.get("material_id");
        String materialNumStr = data.get("material_num");
        Integer materialNum;
        Integer foodId;
        Integer materialId;
        try {
            foodId = Integer.parseInt(foodIdStr);
            materialId = Integer.parseInt(materialIdStr);
            materialNum = Integer.parseInt(materialNumStr);
        } catch (NumberFormatException e) {
            return Result.error("菜品或原材料 数据格式有误");
        }

        if (materialNum < 0  || materialNum > 10000) {
            return Result.error("食材数量有误");
        }

        Food curFood = foodMapper.selectById(foodId);
        Material curMaterial = materialMapper.selectById(materialId);

        if (curFood == null) {
            return Result.error("菜品不存在或已被删除");
        }
        if (curMaterial == null) {
            return Result.error("原材料不存在或已被删除");
        }

//        QueryWrapper<FoodMaterial> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("food_id", foodId);
//        queryWrapper.eq("material_id", materialId);
//        List<FoodMaterial> foodMaterialList = foodMaterialMapper.selectList(queryWrapper);
//        if (!foodMaterialList.isEmpty()) {
//            return Result.error("当前 菜品-原材料 关系已存在");
//        }

        cur.setFoodId(foodId);
        cur.setMaterialId(materialId);
        cur.setFoodName(curFood.getName());
        cur.setMaterialName(curMaterial.getName());
        cur.setMaterialNum(materialNum);

        foodMaterialMapper.updateById(cur);

        return Result.ok();
    }

    @Override
    public Result getAll() {
        List<FoodMaterial> foodMaterialList = foodMaterialMapper.selectList(null);
        return Result.ok(foodMaterialList);
    }

    @Override
    public Result getFoodMaterialListByFoodId(Map<String, String> data) {
        String foodIdStr = data.get("food_id");
        Integer foodId;
        try {
            foodId = Integer.parseInt(foodIdStr);
        } catch (Exception e) {
            return Result.error("foodId数据类型有误");
        }
        QueryWrapper<FoodMaterial> foodMaterialQueryWrapper = new QueryWrapper<>();
        foodMaterialQueryWrapper.eq("food_id", foodId);
        List<FoodMaterial> foodMaterialList = foodMaterialMapper.selectList(foodMaterialQueryWrapper);
        return Result.ok(foodMaterialList);
    }
}
